package com.bball.court.controller;

import java.util.concurrent.CountDownLatch;

import android.test.InstrumentationTestCase;
import android.util.Log;

import com.bball.court.controller.play.PlayPlayerController4Test;
import com.bball.court.http.AddPlayerToCourtRequestCallbacks;
import com.bball.court.http.DeletePlayerToCourtRequestCallbacks;
import com.bball.court.model.Court;

public class PlayerControllerTest extends InstrumentationTestCase {

    private PlayPlayerController4Test playerController;
    private CountDownLatch signal;

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        playerController = new PlayPlayerController4Test(getInstrumentation().getTargetContext());
    }

    public void testCheckin() throws InterruptedException {
        signal = new CountDownLatch(1);
        playerController.checkInCourt("T0sGBF072B", new MockAddPlayerToCourtRequestCallbacks(1));
        signal.await();
    }

    public void testLeave() throws InterruptedException {
        signal = new CountDownLatch(1);
        playerController.leaveCourt("T0sGBF072B", new MockDeletePlayerToCourtRequestCallbacks(0));
        signal.await();
    }

    private class MockAddPlayerToCourtRequestCallbacks implements AddPlayerToCourtRequestCallbacks {

        private int cpt;

        public MockAddPlayerToCourtRequestCallbacks(int cpt) {
            this.cpt = cpt;
        }

        @Override
        public void onAddPlayerSuccess(Court court) {
            assertEquals(cpt, court.getPlayerCount().intValue());
            signal.countDown();
        }

        @Override
        public void onAddPlayerFail(RuntimeException runtimeException) {
            Log.e(PlayerControllerTest.class.getSimpleName(), runtimeException.getMessage());
            fail();
        }
    };

    private class MockDeletePlayerToCourtRequestCallbacks implements DeletePlayerToCourtRequestCallbacks {

        private int cpt;

        public MockDeletePlayerToCourtRequestCallbacks(int cpt) {
            this.cpt = cpt;
        }

        @Override
        public void onDeletePlayerSuccess(Court court) {
            assertEquals(cpt, court.getPlayerCount().intValue());
            signal.countDown();
        }

        @Override
        public void onDeletePlayerFail(RuntimeException runtimeException) {
            Log.e(PlayerControllerTest.class.getSimpleName(), runtimeException.getMessage());
            fail();
        }
    };

}
